<template>
  <el-container class="level-view panel-block">
    <el-header class="sa-header">
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">
          <span class="left">分销商等级</span>
        </div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="getData()"></el-button>
          <el-button
            v-auth="'shop.admin.commission.level.add'"
            icon="Plus"
            type="primary"
            @click="onAdd"
            >添加等级</el-button
          >
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0" v-loading="state.loading">
      <el-table height="100%" class="sa-table" :data="state.tableData" stripe>
        <template #empty>
          <sa-empty />
        </template>
        <el-table-column label="等级" min-width="90">
          <template #default="scope"> 等级{{ scope.row.level }} </template>
        </el-table-column>
        <el-table-column label="等级名称" min-width="150" align="center">
          <template #default="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="等级徽章" min-width="120" align="center">
          <template #default="scope">
            <sa-image :url="scope.row.image" size="24"></sa-image>
          </template>
        </el-table-column>
        <el-table-column label="一级(自购)佣金比例" min-width="160" align="center">
          <template #default="scope"> {{ scope.row.commission_rules?.commission_1 }}% </template>
        </el-table-column>
        <el-table-column label="二级佣金比例" min-width="120" align="center">
          <template #default="scope"> {{ scope.row.commission_rules?.commission_2 }}% </template>
        </el-table-column>
        <el-table-column label="三级佣金比例" min-width="120" align="center">
          <template #default="scope"> {{ scope.row.commission_rules?.commission_3 }}% </template>
        </el-table-column>
        <el-table-column label="操作" min-width="120" fixed="right">
          <template #default="scope">
            <div class="sa-flex">
              <el-button
                v-auth="'shop.admin.commission.level.detail'"
                class="is-link"
                type="primary"
                @click="onEdit(scope.row.level)"
                >编辑</el-button
              >
              <el-popconfirm
                width="fit-content"
                confirm-button-text="确认"
                cancel-button-text="取消"
                title="确认删除这条记录?"
                @confirm="onDelete(scope.row.level)"
              >
                <template #reference>
                  <div class="sa-m-l-12">
                    <el-button
                      v-if="scope.row.level != 1"
                      v-auth="'shop.admin.commission.level.delete'"
                      class="is-link"
                      type="danger"
                    >
                      删除
                    </el-button>
                  </div>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </el-container>
</template>
<script>
  export default {
    name: 'shop.admin.commission.level',
  };
</script>
<script setup>
  import { onMounted, reactive } from 'vue';
  import { api } from '../commission.service';
  import { useModal } from '@/sheep/hooks';
  import LevelEdit from './edit.vue';
  // 表格
  const state = reactive({
    loading: false,
    tableData: [],
  });

  // 获取数据
  async function getData() {
    state.loading = true;
    const { error, data } = await api.level.list({
      sort: 'level',
      order: 'asc',
    });
    if (error === 0) {
      state.tableData = data;
    }
    state.loading = false;
  }

  function onAdd() {
    useModal(
      LevelEdit,
      {
        title: '添加等级',
        type: 'add',
      },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }

  function onEdit(id) {
    useModal(
      LevelEdit,
      {
        title: '编辑等级',
        type: 'edit',
        id,
      },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }

  async function onDelete(level) {
    await api.level.delete(level);
    getData();
  }

  onMounted(() => {
    getData();
  });
</script>
<style lang="scss" scoped>
  .level-view {
    .sa-table {
      .sa-image {
        display: inline-block;
      }
    }
  }
</style>
